package com.eqs.enterprisequestionnaire.controller;

import com.eqs.enterprisequestionnaire.model.pojo.Answer;
import com.eqs.enterprisequestionnaire.model.pojo.Response;
import com.eqs.enterprisequestionnaire.result.R;
import com.eqs.enterprisequestionnaire.service.ResponseService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/response")
public class ResponseController {
    @Resource
    private ResponseService responseService;
    /**
     * 新增（设置提交时间和更新时间，相当于提交）
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody Response response){
        responseService.add(response);
        return R.success("success");
    }
    /*
    * 开始作答(仅设置开始时间)
    * */
    @PostMapping("/start")
    public R<Response> start(@RequestBody Response response){
        Response r = responseService.start(response);
        return R.success(r);
    }

    /**
     * 删除(根据id)
     */
    @DeleteMapping("/delete/{id}")
    public R<String> deleteById(Integer id) {
        responseService.removeById(id);
        return R.success("success");
    }
    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public R<String> deleteBatch(@RequestBody List<Integer> ids){
        responseService.removeByIds(ids);
        return R.success("success");
    }
    /**
     * 修改(根据id)
     */
    @PutMapping("/update")
    public R<String> update(@RequestBody Response response){
        responseService.updateResponse(response);
        return R.success("success");
    }
    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public R<Response> selectById(Integer id){
        Response response = responseService.getById(id);
        return R.success(response);
    }
    /**
     * 查询所有(可根据属性查询出list)
     */
    @GetMapping("/selectAll")
    public R<List<Response>> selectAll(@RequestBody Response response){
        List<Response> responses =  responseService.selectAll(response);
        return R.success(responses);
    }

    /*
    * 提交问卷
    * */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Response response){
        List<Answer> answers=response.getAnswers();
        responseService.submit(response,answers);
        return R.success("success");
    }
    /*
     * 修改答卷
     * */
    @PutMapping("/updated")
    public R<String> updated(@RequestBody Response response){
        List<Answer> answers=response.getAnswers();
        responseService.updated(response,answers);
        return R.success("success");
    }
    @PostMapping("/submitWhole")
    public R submitWhole(@RequestBody Response response){
        Response result=responseService.submitWholeResponse(response);
        if(result.getId()==0){
            return R.error("更新失败");
        }
        else {
            return R.success(result);
        }
    }

    @GetMapping("/getWholeResponse")
    public R getWholeResponse(@RequestParam Integer userId,@RequestParam Integer questionnaireId){
        return R.error("未定义");
    }
    @PutMapping("/updateWhole")
    public R updateWholeResponse(@RequestBody Response response){
        return R.success(responseService.updateWholeResponse(response));
    }

    @GetMapping("/getResponseCount/{questionId}")
    public R<Integer> getResponseCount(@PathVariable Integer questionId){
        return R.success(responseService.getResponseCountByQuestionId(questionId));
    }


}
